package cn.iocoder.yudao.module.squaredancing.service.download;

import cn.iocoder.yudao.module.squaredancing.controller.app.download.vo.DownloadPageReqVO;
import cn.iocoder.yudao.module.squaredancing.controller.app.download.vo.DownloadSaveReqVO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import cn.iocoder.yudao.module.squaredancing.dal.dataobject.download.DownloadDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.squaredancing.dal.mysql.download.DownloadMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.squaredancing.enums.ErrorCodeConstants.DOWNLOAD_NOT_EXISTS;

/**
 * 视频下载 Service 实现类
 *
 * @author 赵云安
 */
@Service
@Validated
public class DownloadServiceImpl implements DownloadService {

    @Resource
    private DownloadMapper downloadMapper;

    @Override
    public Long createDownload(DownloadSaveReqVO createReqVO) {
        // 插入
        DownloadDO download = BeanUtils.toBean(createReqVO, DownloadDO.class);
        downloadMapper.insert(download);

        // 返回
        return download.getId();
    }

    @Override
    public void updateDownload(DownloadSaveReqVO updateReqVO) {
        // 校验存在
        validateDownloadExists(updateReqVO.getId());
        // 更新
        DownloadDO updateObj = BeanUtils.toBean(updateReqVO, DownloadDO.class);
        downloadMapper.updateById(updateObj);
    }

    @Override
    public void deleteDownload(Long id) {
        // 校验存在
        validateDownloadExists(id);
        // 删除
        downloadMapper.deleteById(id);
    }

    @Override
        public void deleteDownloadListByIds(List<Long> ids) {
        // 删除
        downloadMapper.deleteByIds(ids);
        }


    private void validateDownloadExists(Long id) {
        if (downloadMapper.selectById(id) == null) {
            throw exception(DOWNLOAD_NOT_EXISTS);
        }
    }

    @Override
    public DownloadDO getDownload(Long id) {
        return downloadMapper.selectById(id);
    }

    @Override
    public PageResult<DownloadDO> getDownloadPage(DownloadPageReqVO pageReqVO) {
        return downloadMapper.selectPage(pageReqVO);
    }

}